package com.wisdudu.blank.ui.login;

import android.databinding.ObservableField;
import android.os.CountDownTimer;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextErrorListener;
import com.wisdudu.blank.databinding.FragmentGetCodeBinding;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;
import com.wisdudu.frame.support.util.RegularUtil;

/**
 * Created by ypp on 2016/10/19.
 */
public class GetCodeVM implements ViewModel {
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> code1 = new ObservableField<>();
    public ObservableField<String> code2 = new ObservableField<>();
    public ObservableField<String> code3 = new ObservableField<>();
    public ObservableField<String> code4 = new ObservableField<>();
    public String one = "1";
    public String two = "2";
    public String three = "3";
    public String four = "4";
    public String five = "5";
    public String six = "6";
    public String seven = "7";
    public String eight = "8";
    public String nine = "9";
    public String zero = "0";
    public String delete = "delete";
    private UserRepo mUserRepo;
    private String code;
    private String inputCode = "";
    public VerifyCodeReTimer mVerifyCodeReTimer;
    private FragmentGetCodeBinding mBinding;
    private GetCodeFragment mFragment;

    public GetCodeVM(GetCodeFragment mFragment, FragmentGetCodeBinding mBinding, String phone, String code) {
        mUserRepo = Injection.provideUserRepo();
        this.mFragment = mFragment;
        this.mBinding = mBinding;
        this.phone.set(phone);
        this.code = code;
        mVerifyCodeReTimer = new VerifyCodeReTimer(30000, 1000);
        mVerifyCodeReTimer.start();
    }

    public ReplyCommand<String> onNumberClick = new ReplyCommand<String>((idName) -> {
        if (!idName.equals(delete)) {
            if (TextUtils.isEmpty(inputCode) || inputCode.length() < 4) {
                inputCode += idName;
            }
        } else {
            if (inputCode.length() > 1) {
                inputCode = inputCode.toString().substring(0, inputCode.length() - 1);
            } else {
                inputCode = "";
            }
        }
        add();
    });
    public ReplyCommand onGetCodeClick = new ReplyCommand(() -> {
        getSmsCode();
    });

    public ReplyCommand onConfirmClick = new ReplyCommand(() -> {
        if (TextUtils.isEmpty(code1.get()) || TextUtils.isEmpty(code2.get()) || TextUtils.isEmpty(code3.get()) || TextUtils.isEmpty(code4.get())) {
            ToastUtil.INSTANCE.toast("请输入验证码");
            return;
        }
        inputCode = code1.get() + code2.get() + code3.get() + code4.get();
        int sCode = (int) Double.parseDouble(code);
        if (Integer.parseInt(inputCode) == sCode) {
            mFragment.addFragment(PasSetFragment.newIntance(phone.get(), inputCode));
        } else {
            ToastUtil.INSTANCE.toast("验证码错误");
            return;
        }
    });

    private void getSmsCode() {
        if (TextUtils.isEmpty(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码不能为空");
            return;
        }
        if (!RegularUtil.isMobile(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码格式错误");
            return;
        }
        mUserRepo.getSmsCode(phone.get(), 1).subscribe(new ProgressSubscriber<>(new SubscriberOnNextErrorListener<Object>() {

            @Override
            public void onError(Throwable e, LoadingProgressDialog pd) {
                ToastUtil.INSTANCE.toast(e.getMessage());
            }

            @Override
            public void onNext(Object aLong, LoadingProgressDialog pd) {
                pd.setTitle("发送成功");
                code = String.valueOf(aLong);
                mVerifyCodeReTimer = new VerifyCodeReTimer(30000, 1000);
                mVerifyCodeReTimer.start();
            }
        }, mFragment.getActivity(), "正在发送...", 1000));
    }

    private void add() {
        if (inputCode.length() == 0) {
            code1.set("");
            code2.set("");
            code3.set("");
            code4.set("");
        }
        if (inputCode.length() == 1) {
            code1.set(inputCode.toString().substring(0, 1));
            code2.set("");
            code3.set("");
            code4.set("");
        }
        if (inputCode.length() == 2) {
            code1.set(inputCode.toString().substring(0, 1));
            code2.set(inputCode.toString().substring(1, 2));
            code3.set("");
            code4.set("");
        }
        if (inputCode.length() == 3) {
            code1.set(inputCode.toString().substring(0, 1));
            code2.set(inputCode.toString().substring(1, 2));
            code3.set(inputCode.toString().substring(2, 3));
            code4.set("");
        }
        if (inputCode.length() == 4) {
            code1.set(inputCode.toString().substring(0, 1));
            code2.set(inputCode.toString().substring(1, 2));
            code3.set(inputCode.toString().substring(2, 3));
            code4.set(inputCode.toString().substring(3, 4));
        }
    }

    class VerifyCodeReTimer extends CountDownTimer {

        public VerifyCodeReTimer(long millisUntilFinished, long countDownInterval) {
            super(millisUntilFinished, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mBinding.tvGetcode.isClickable()) {
                mBinding.tvGetcode.setClickable(false);
            }
            mBinding.tvGetcode.setText("重新发送(" + String.valueOf(millisUntilFinished / 1000) + ")");
        }

        @Override
        public void onFinish() {
            if (!mBinding.tvGetcode.isClickable()) {
                mBinding.tvGetcode.setClickable(true);
            }
            mBinding.tvGetcode.setText("重新发送验证码");
        }
    }

}
